using System;
using System.Collections;
using System.Web;
using com.paypal.soap.api;
using System.Net;
using System.IO;

namespace ASPDotNetSamples {
    /// <summary>
    /// Summary description for Util.
    /// </summary>
    public class Util {
        public static string AppendTransactionId(string page, string transactionId) {
            return page + "?" + Constants.TRANSACTIONID_PARAM + "=" + transactionId;
        }

        public static bool IsSuccessfull(AckCodeType ack) {
            return ack == AckCodeType.Success || ack == AckCodeType.SuccessWithWarning;
        }

        public static string ToTitleCase(string inputString)
        {
            System.Globalization.CultureInfo cultureInfo =
            System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Globalization.TextInfo textInfo = cultureInfo.TextInfo;
            return textInfo.ToTitleCase(inputString.ToLower());
        }

        public static string GenerateCreditCardNumber(string cardType) {
            int[] cc_number = new int[16];
            int cc_len = 16;
            int start = 0;
            Random r = new Random();
            switch (cardType) {
                case "Visa":
                    cc_number[start++] = 4;
                    break;
                case "Discover":
                    cc_number[start++] = 6;
                    cc_number[start++] = 0;
                    cc_number[start++] = 1;
                    cc_number[start++] = 1;
                    break;
                case "MasterCard":
                    cc_number[start++] = 5;
                    cc_number[start++] = (int)Math.Floor(r.NextDouble() * 5) + 1;
                    break;
                case "Amex":
                    cc_number = new int[15];
                    cc_number[start++] = 3;
                    cc_number[start++] = r.Next(2) == 1 ? 7 : 4;
                    cc_len = 15;
                    break;
            }

            for (int i = start; i < (cc_len - 1); i++) {
                cc_number[i] = (int)Math.Floor(r.NextDouble() * 10);
            }

            int sum = 0;
            for (int j = 0; j < (cc_len - 1); j++) {
                int digit = cc_number[j];
                if ((j & 1) == (cc_len & 1)) digit *= 2;
                if (digit > 9) digit -= 9;
                sum += digit;
            }

            int[] check_digit = new int[] { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
            cc_number[cc_len - 1] = check_digit[sum % 10];

            string result = string.Empty;
            foreach (int digit in cc_number) {
                result += digit;
            }
            return result;
        }

        public static IEnumerable SupportedCurrencies() {
            return new CurrencyCodeType[] { CurrencyCodeType.USD, CurrencyCodeType.GBP, CurrencyCodeType.JPY, CurrencyCodeType.CAD, CurrencyCodeType.AUD, CurrencyCodeType.EUR };
        }

        public static string GetShortUrl(string originalUrl) {
            string requestUrl =
                "http://api.bitly.com/v3/shorten?login=tmhdev01&apiKey=R_31c559b98386fed5f14a11322e49d747&longUrl=" + originalUrl + "&format=txt";
            WebRequest request = WebRequest.Create(requestUrl);
            WebResponse response = request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            return reader.ReadToEnd();
        }

        public static string RewriteUrl(string longUrl) {
            string shortUrl = longUrl;

            try {

                if (longUrl.Contains("/Default.aspx?ReferUserID=")) {
                    string mainPath = longUrl.Substring(0, longUrl.IndexOf("/Default.aspx?ReferUserID="));

                    int index = longUrl.IndexOf('=');
                    string userId = longUrl.Substring(index + 1, Guid.Empty.ToString().Length);
                    index = longUrl.IndexOf('=', index + 1);
                    string cityId = longUrl.Substring(index + 1, longUrl.Length - index - 1);

                    shortUrl = mainPath + "/r.aspx/" + userId.Replace("-", "") + "/" + cityId;
                }

            } catch (Exception) {
                shortUrl = longUrl;
            }

            return shortUrl;
        }

        public static string RevertUrl(string shortUrl) {
            string longUrl = shortUrl;

            try {

                if (shortUrl.Contains("/r.aspx/")) {
                    string mainPath = shortUrl.Substring(0, shortUrl.IndexOf("/r.aspx/"));
                    string parametersString = shortUrl.Substring(shortUrl.IndexOf("/r.aspx/") + 3);
                    if (!string.IsNullOrEmpty(parametersString)) {
                        string[] parameters = parametersString.Split('/');
                        if (parameters.Length == 2) {
                            Guid userId = new Guid(parameters[0]);
                            int cityId = int.Parse(parameters[1]);

                            longUrl = mainPath + "/Default.aspx?ReferUserID=" + userId + "&CityID=" + cityId;
                        }
                    }
                }

            } catch (Exception) {
                longUrl = shortUrl;
            }

            return longUrl;
        }
    }
}
